/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author
 */
public class NguoiDung {
    //Danh sach thuoc tinh

    private String userName;
    private String password;
    private String hoTen;
    private int loaiNguoiDung;

    //Danh sach ham get va set
    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getHoTen() {
        return hoTen;
    }

    public void setHoTen(String hoTen) {
        this.hoTen = hoTen;
    }

    public int getLoaiNguoiDung() {
        return loaiNguoiDung;
    }

    public void setLoaiNguoiDung(int loaiNguoiDung) {
        this.loaiNguoiDung = loaiNguoiDung;
    }
    //Danh sach ham khoi tao

    public NguoiDung(String userName, String password, String hoTen, int loaiNguoiDung) {
        this.userName = userName;
        this.password = password;
        this.hoTen = hoTen;
        this.loaiNguoiDung = loaiNguoiDung;
    }

    public NguoiDung() {
    }

    //Ham lay danh sach nguoi dung
    public static List<NguoiDung> layDanhSachNguoiDung() {
        List<NguoiDung> danhSach = new ArrayList<NguoiDung>();
        String sql = "select * from nguoi_dung";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve result set sau khi truy van du lieu
            ResultSet rs = statement.executeQuery();
            //Lap lay tung dong du lieu
            while (rs.next()) {

                NguoiDung nd = new NguoiDung();
                nd.userName = rs.getString("user_name");
                nd.password = rs.getString("password");
                nd.hoTen = rs.getString("ho_ten");
                nd.loaiNguoiDung = rs.getInt("loai_nguoi_dung");
                danhSach.add(nd);

            }
            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }
        return danhSach;
    }

    //Ham lay nguoi dung theo username
    public static NguoiDung layNguoiDungTheoUsername(String username) {
        NguoiDung nd = null;
        String sql = "select * from nguoi_dung where user_name='" + username + "'";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve result set sau khi truy van du lieu
            ResultSet rs = statement.executeQuery();
            //Lap lay tung dong du lieu
            while (rs.next()) {

                nd = new NguoiDung();
                nd.userName = rs.getString("user_name");
                nd.password = rs.getString("password");
                nd.hoTen = rs.getString("ho_ten");
                nd.loaiNguoiDung = rs.getInt("loai_nguoi_dung");
            }
            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }
        return nd;
    }

    //Ham them nguoi dung
    public boolean ThemNguoiDung() {
        boolean rs=false;
        String sql = "insert into nguoi_dung(user_name,password,ho_ten,loai_nguoi_dung) values(";
        sql += "'" + this.userName + "','" + this.password + "','" + this.hoTen + "'," + this.loaiNguoiDung + " )";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve resulrs = true;t set sau khi truy van du lieu
            
            if(statement.executeUpdate() == 1){
                rs=true;
            }

            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }

        return rs;
    }
    
     //Ham cap nhat nguoi dung
    public boolean CapNhatNguoiDung() {
        boolean rs=false;
        String sql = "update nguoi_dung set password='"+this.password+"',ho_ten='"+this.hoTen+"',loai_nguoi_dung="+this.loaiNguoiDung+" where user_name='"+this.userName+"'";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve resulrs = true;t set sau khi truy van du lieu
            
            if(statement.executeUpdate() == 1){
                rs=true;
            }

            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }

        return rs;
    }
    
    //Ham xoa nguoi dung
      public static boolean XoaNguoiDung(String user_name) {
        boolean rs=false;
        String sql = "delete from nguoi_dung where user_name='"+user_name+"'";
        try {
            //Tao bien ket noi
            Connection connection = KetNoiCSDL.open();
            // Tao bien statement de truy van
            PreparedStatement statement = (PreparedStatement) connection.prepareStatement(sql);
            //Tra ve resulrs = true;t set sau khi truy van du lieu
            
            if(statement.executeUpdate() == 1){
                rs=true;
            }

            connection.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            KetNoiCSDL.close();
        } finally {
            KetNoiCSDL.close();
        }

        return rs;
    }
}
